package com.jtjrenren.android.taxi.driver.core.splash;

import android.Manifest;
import android.view.View;


import com.jtjrenren.android.taxi.driver.base.MvpActivity;
import com.jtjrenren.android.taxi.driver.utils.IntentUtils;
import com.jtjrenren.android.taxi.driver.R;
import com.jtjrenren.android.taxi.driver.bean.api.ApiUpVersion;
import com.library.utils.LogUtil;
import com.library.weiget.UpAppDlg;
import com.jtjrenren.android.taxi.driver.utils.UIHelper;
import com.library.utils.AppUtils;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;

import rx.Observer;

/**
 * Created by Administrator on 2016/5/3.
 */
public class AppSplashActivity extends MvpActivity<SplashPresenter> implements SplashView {
    private UpAppDlg upVersionDlg;

    @Override
    public int getLayoutID() {
        return R.layout.aty_app_splash;
    }

    @Override
    public void initM() {
        checkPermissions();
    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.sub();
    }

    @Override
    protected void onStop() {
        super.onStop();
        presenter.unSub();
    }

    private void checkPermissions() {
        RxPermissions.getInstance(this)
                .request(Manifest.permission.ACCESS_COARSE_LOCATION
                        , Manifest.permission.ACCESS_FINE_LOCATION
                        , Manifest.permission.ACCESS_WIFI_STATE
                        , Manifest.permission.ACCESS_NETWORK_STATE
                        , Manifest.permission.CHANGE_WIFI_STATE
                        , Manifest.permission.READ_PHONE_STATE
                        , Manifest.permission.WRITE_EXTERNAL_STORAGE
                )
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.log("debug", "权限:" + e.toString());
                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        if (aBoolean) {
                            presenter.checkAppVersion();
                        } else {
                            LogUtil.log("debug", "权限不被允许");
                            displayShort("定位相关被禁止，请关闭应用，并前往设置-应用管理-权限管理开启");
                        }
                    }
                });
    }


    @Override
    public SplashPresenterImpl createPresenter() {
        return new SplashPresenterImpl(this);
    }


    @Override
    public void showUpAppTip(final ApiUpVersion.VersionEntity version, final boolean isForce) {
        if (version == null) {
            //检查更新完毕处理
            presenter.checkLogin(AppSplashActivity.this);
            return;
        }
        upVersionDlg = UIHelper.getUpVersionDlg(this);
        upVersionDlg.setCommon(version.getVersionNO(), "", version.getContent(), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isForce)
                    presenter.checkLogin(AppSplashActivity.this);
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upVersionDlg.showProcess();
                presenter.downApp(version.getAndroidurl());
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isForce)
                    presenter.checkLogin(AppSplashActivity.this);
            }
        });
        upVersionDlg.setCanceledOnTouchOutside(false);
        upVersionDlg.show();
    }

    @Override
    public void showDowning(int process) {
        if (upVersionDlg != null)
            upVersionDlg.refreshProcess(process);
    }

    @Override
    public void downFinish(String apk_path) {
        if (upVersionDlg != null)
            upVersionDlg.dismiss();
        AppUtils.installApk(new File(apk_path), this);
        finish();
    }

    @Override
    public void splashFinish(int type) {
        if (type == TYPE_LOGIN)
            IntentUtils.goLoginPage(AppSplashActivity.this);
        else if (type == TYPE_Main)
            IntentUtils.goMainActivity(AppSplashActivity.this);
        finish();
    }

}
